package com.example.administrator.smartbutler.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.smartbutler.R;
import com.example.administrator.smartbutler.entity.MyUser;
import com.example.administrator.smartbutler.utils.ErroeCodeUtils;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 项目名:SmartButler
 * 包名:com.example.administrator.smartbutler.ui
 * 文件名：forgetPasswordActivity
 * 创建者：justin
 * Created by Administrator on 2018/1/25.
 * 描述：TODO
 */

public class forgetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private Button updatePwd;

    private EditText oldPwd;

    private EditText newPwd;

    private EditText again;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd);

        //初始化数据
        initView();
    }

    private void initView() {

        oldPwd = (EditText) findViewById(R.id.forgetPassword_oldPwd);
        newPwd = (EditText) findViewById(R.id.forgetPassword_newPwd);

        again = (EditText) findViewById(R.id.forgetPassword_again);


        updatePwd = (Button) findViewById(R.id.forgetPassword_updatePwd);

        updatePwd.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.forgetPassword_updatePwd:

                String old = oldPwd.getText().toString().trim();
                String now = newPwd.getText().toString().trim();
                String againPwd = again.getText().toString().trim();

                //先判断所有的值是否为空
                if (!TextUtils.isEmpty(old) & !TextUtils.isEmpty(now) & !TextUtils.isEmpty(againPwd)) {

                    //再判断新旧密码是否一致

                    if (now.equals(againPwd)) {

                        //重置密码
                        MyUser.updateCurrentUserPassword(old, now, new UpdateListener() {
                            @Override
                            public void done(BmobException e) {

                                if (e == null) {
                                    Toast.makeText(forgetPasswordActivity.this, "密码修改成功，可以用新密码进行登录啦", Toast.LENGTH_SHORT).show();


                                } else {
                                    Toast.makeText(forgetPasswordActivity.this, "密码修改失败" + ErroeCodeUtils.findError(e.getErrorCode()), Toast.LENGTH_SHORT).show();
                                }

                            }
                        });


                    } else {
                        Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    }


                } else {
                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                }


                break;

        }

    }
}
